<template>
  <div class="box">
    <a-back-top>
      <div class="ant-back-top-inner">UP</div>
    </a-back-top>

    <a-form
      ref="formRef"
      :model="formState"
      name="basic"
      autocomplete="off"
      @finish="onFinish"
      @finishFailed="onFinishFailed"
    >
      <a-row :gutter="[16, 16]">
        <a-col :sm="12" :md="12" :lg="6">
          <a-form-item label="标题" name="title">
            <a-input v-model:value="formState.title" placeholder="请输入标题" />
          </a-form-item>
        </a-col>
        <a-col :sm="12" :md="12" :lg="6">
          <a-form-item label="时间" name="createDate">
            <a-date-picker
              placeholder="请选择时间"
              v-model:value="formState.createDate"
              style="min-width: 100%"
              @change="handeOnchange"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="12" :md="12" :lg="6">
          <a-form-item label="类别" name="opinionsType">
            <a-select
              ref="select"
              v-model:value="formState.opinionsType"
              style="width: 100%"
              placeholder="请选择类别"
            >
              <a-select-option value="1">新闻</a-select-option>
              <a-select-option value="2">言论</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="12" :md="12" :lg="6">
          <a-form-item label="正文" name="content">
            <a-input
              v-model:value="formState.content"
              placeholder="请输入正文"
            />
          </a-form-item>
        </a-col>

        <!-- <a-checkbox v-model:checked="formState.remember">Remember me</a-checkbox> -->

        <a-col :sm="12" :md="12" :lg="8">
          <a-button type="primary" html-type="submit">搜索</a-button>
          <a-button @click="reset" style="margin-left: 20px">重置</a-button>
        </a-col>

        <a-col :span="24">
          <a-list
            item-layout="vertical"
            :data-source="data"
            bordered
            :loading="initLoading"
          >
            <template #loadMore>
              <div
                v-if="!initLoading && !loading && data.length > 0"
                :style="{
                  textAlign: 'center',
                  margin: '12px 0',
                  height: '32px',
                  lineHeight: '32px',
                }"
              >
                <a-button @click="onLoadMore">
                  <template #icon><SearchOutlined /></template>loading
                  more</a-button
                >
              </div>
            </template>
            <template #renderItem="{ item }">
              <a-list-item>
                <template #actions>
                  <a key="list-loadmore-edit" @click="handleDetail(item.id)"
                    >详情</a
                  >
                  <a-popconfirm
                    title="确定是否删除"
                    ok-text="是"
                    cancel-text="否"
                    @confirm="confirm(item.id)"
                  >
                    <a key="list-loadmore-more">删除</a>
                  </a-popconfirm>

                  <span v-for="{ type, text } in actions" :key="type">
                    {{ text }} :
                    <span>{{
                      type === "createDate"
                        ? dayjs(item[type]).format("YYYY-MM-DD HH:mm:ss")
                        : soureType[item[type]]
                    }}</span>
                  </span>
                </template>
                <a-list-item-meta>
                  <template #title>
                    <span>{{ item.title }}</span>
                  </template>
                </a-list-item-meta>
                <div class="test-content">
                  {{ item.content }}
                </div>
              </a-list-item>
            </template>
          </a-list>
        </a-col>
      </a-row>
    </a-form>
    <DetailModal ref="detail" />
  </div>
</template>

<script setup>
import { onMounted, reactive, ref, toRefs } from "vue";
import { findByPage, deleteItem, detailItem } from "@/api/yqList";
import DetailModal from "./component/detailModal.vue";
import dayjs from "dayjs";

const soureType = {
  1: "百度",
  2: "腾讯",
  3: "新浪",
  4: "微博",
  5: "抖音",
};

const formState = reactive({
  createDate: "",
  title: "",
  opinionsType: null,
  content: "",
  page: 1,
});

const formRef = ref(null);
const detail = ref(null);

let data = reactive([]);

const actions = [
  { type: "createDate", text: "时间" },
  { type: "opinionsType", text: "类别" },
];
const onFinish = (value) => {
  loading.value = true;
  initLoading.value = true;
  findByPage(formState)
    .then((res) => {
      const { code } = res;
      if (code === 200) {
        data = res.data.data;
      }
    })
    .finally(() => {
      loading.value = false;
      initLoading.value = false;
    });
};
const reset = () => {
  formRef.value.resetFields();
  formState.page = 1;
  onFinish();
};
const onFinishFailed = () => {};
const initLoading = ref(false);
const loading = ref(false);
const pagination = {
  onChange: (page) => {},
  pageSize: 3,
};

const onLoadMore = () => {
  loading.value = true;
  initLoading.value = true;

  const { page } = toRefs(formState);
  formState.page = ++page.value;
  findByPage(formState).then((res) => {
    const { code } = res;
    if (code === 200) {
      loading.value = false;
      initLoading.value = false;
      data = data.concat(res.data.data);
    }
  });
};
// const handleGoDetail = () => {
//   router.push("/detail/detail");
// };
const confirm = (id) => {
  deleteItem(id).then((res) => {
    const { code = null } = res;
    if (code === 200) {
      formState.page = 1;
      onFinish();
    }
  });
};
const handleDetail = (id) => {
  detailItem(id).then((res) => {
    const { code = null } = res;
    if (code === 200) {
      detail.value.open(res.data);
    }
  });
};
const handeOnchange = (value) => {
  formState.createDate = dayjs(value).format("YYYY-MM-DD");
};
onMounted(() => {
  onFinish();
});
</script>

<style lang="scss" scoped>
.ant-back-top {
  bottom: 100px;
}
.box {
  padding: 20px;
  background: #fff;
  box-shadow: 0 1px 2px -2px #00000029, 0 3px 6px #0000001f,
    0 5px 12px 4px #00000017;
  border-radius: 10px;
}
.ant-back-top-inner {
  height: 40px;
  width: 40px;
  line-height: 40px;
  border-radius: 4px;
  background-color: #1088e9;
  color: #fff;
  text-align: center;
  font-size: 20px;
}
.test-content {
  // word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3; //显示几行
  overflow: hidden;

  // //有时候 超出显示多行显示省略号会不生效，则需要下面代码
  // word-break: break-all;
  // text-overflow: ellipsis;
  // display: -webkit-box;
  // /*! autoprefixer: off */
  // -webkit-box-orient: vertical;
  // /* autoprefixer: on */
  // -webkit-line-clamp: 3; //显示几行
  // overflow: hidden;
}
</style>
